package com.spzx.order.controller;

import com.github.pagehelper.PageHelper;
import com.spzx.common.core.context.SecurityContextHolder;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.order.domain.OrderInfo;
import com.spzx.order.form.OrderForm;
import com.spzx.order.service.OrderInfoService;
import com.spzx.order.vo.TradeVo;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-05-09
 */
@RestController
@RequestMapping("/orderInfo")
public class OrderInfoController extends BaseController {


    @Autowired
    private OrderInfoService orderInfoService;


    /**
     * 订单结算页面渲染
     *
     * @return
     */
    @Operation(summary = "订单结算页面渲染")
    @GetMapping("/trade")
    public AjaxResult trade() {
        TradeVo tradeVo = orderInfoService.trade();
        return success(tradeVo);
    }


    /**
     * 提交订单
     *
     * @param orderForm
     * @return 订单ID或者订单编号
     */
    @Operation(summary = "提交订单")
    @PostMapping("/submitOrder")
    public AjaxResult submitOrder(@RequestBody OrderForm orderForm) {
        Long orderId = orderInfoService.submitOrder(orderForm);
        return success(orderId);
    }


    /**
     * 订单结算页面渲染
     *
     * @param skuId
     * @return
     */
    @Operation(summary = "立即购买渲染订单结算页")
    @GetMapping("/buy/{skuId}")
    public AjaxResult buy(@PathVariable("skuId") Long skuId) {
        TradeVo tradeVo = orderInfoService.buy(skuId);
        return success(tradeVo);
    }


    @Operation(summary = "获取订单详情")
    @GetMapping("/getOrderInfo/{orderId}")
    public AjaxResult getOrderInfo(@PathVariable("orderId") Long orderId) {
        OrderInfo orderInfo = orderInfoService.getById(orderId);
        return success(orderInfo);
    }


    /**
     * 分页查询订单列表
     *
     * @param pageNum
     * @param pageSize
     * @param orderStatus
     * @return
     */
    @Operation(summary = "分页查询订单列表")
    @GetMapping("/userOrderInfoList/{pageNum}/{pageSize}")
    public TableDataInfo userOrderInfoList(
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize,
            @RequestParam(required = false) String orderStatus
    ) {
        //启动分页
        PageHelper.startPage(pageNum, pageSize);
        //查询订单列表
        List<OrderInfo> list = orderInfoService.userOrderInfoList(SecurityContextHolder.getUserId(), orderStatus);
        return getDataTable(list);
    }

    @Operation(summary = "取消订单")
    @GetMapping("/cancelOrder/{orderId}")
    public AjaxResult cancelOrder(@PathVariable Long orderId) {
        orderInfoService.cancelOrder(orderId);
        return success();
    }

    @InnerAuth
    @Operation(summary = "根据订单号获取订单信息")
    @GetMapping("/getByOrderNo/{orderNo}")
    public R<OrderInfo> getByOrderNo(@PathVariable String orderNo) {
        OrderInfo orderInfo = orderInfoService.getByOrderNo(orderNo);
        return R.ok(orderInfo);
    }
}
